package net.ib.mtalk.ui.start;

import java.util.Vector;

import net.ib.mtalk.R;
import net.ib.mtalk.constant.MTalkIntent;
import net.ib.mtalk.service.SelecterService;
import net.ib.mtalk.util.NameSearch;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class CountryActivity extends ListActivity implements OnItemClickListener, TextWatcher {
	
	private EditText editCountry;
	private String selectedCountry = null;
	private int selectedIndex = -1;
	private CharSequence[] allCountries;
	private Vector<CharSequence> countries;
	private Context mContext;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_country);
		mContext = this;
		editCountry = (EditText) findViewById(R.id.editCountry);
		editCountry.addTextChangedListener(this);
		allCountries = getResources().getTextArray(R.array.country);
		
		countries = new Vector<CharSequence>();
		for(int i=0 ; i < allCountries.length ; ++i) {
			countries.add(allCountries[i]);
		}
		setListAdapter(new CountryAdapter());
		
		final ListView listView = getListView();
		listView.setItemsCanFocus(false);
		listView.setOnItemClickListener(this);
	}
	
	private class CountryAdapter extends BaseAdapter
	{
		private LayoutInflater mInflater;
		private TextView mText;
		private CheckBox mRadio;
		
		public CountryAdapter()
		{
			mInflater = LayoutInflater.from(mContext);
		}
		
		public int getCount() {
			return countries.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView == null)
			{
				convertView = mInflater.inflate(R.layout.layout_item_country, parent, false);
			} 
			
			mText = (TextView)convertView.findViewById(R.id.country_item_text);
			mText.setText(countries.get(position));
			
			mRadio = (CheckBox)convertView.findViewById(R.id.country_item_radio);
			if(selectedIndex == position)
				mRadio.setChecked(true);
			else
				mRadio.setChecked(false);
			
			return convertView;
		}
		
	}

	public void onClick(View v) {
		switch(v.getId()) {
//		case R.id.buttonSearch:
//			String searchable = editCountry.getText().toString();
//			countries.clear();
//			for(int c = 0 ; c < allCountries.length ; ++c) {
//				int d = searchable.length();
//				if(d <= allCountries[c].length()){
//					if(NameSearch.isMatch(allCountries[c].toString(), searchable)){
//						countries.add(allCountries[c]);
//					}
//				}
//			}
//			((BaseAdapter) getListAdapter()).notifyDataSetChanged();
//			break;
		case R.id.buttonSelect:
			if(selectedCountry != null) {
				Intent i = getIntent().putExtra(MTalkIntent.KEY_COUNTRY_NAME, selectedCountry);
				setResult(RESULT_OK, i);
				finish();
			} else {
				//TODO:나라 선택 안했을 경우
				
			}
			
			break;
		case R.id.buttonCancel:
			setResult(RESULT_CANCELED);
			finish();
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		selectedCountry = (String) ((TextView)view.findViewById(R.id.country_item_text)).getText();
		selectedIndex = position;
		((CountryAdapter)parent.getAdapter()).notifyDataSetChanged();
	}

	/**
	 * 기획에는 검색을 할 경우에만 listview의 Item을 바꾸도록 되어 있지만 실제 사용의 편의를 위해 text 바뀔 때마다 적용해봄.
	 */
	@Override
	public void afterTextChanged(Editable s) {
		String searchable = editCountry.getText().toString();
		countries.clear();
		for(int c = 0 ; c < allCountries.length ; ++c) {
			int d = searchable.length();
			if(d <= allCountries[c].length()){
				if(NameSearch.isMatch(allCountries[c].toString(), searchable)){
					countries.add(allCountries[c]);
				}
			}
		}
		((BaseAdapter) getListAdapter()).notifyDataSetChanged();
	}
	
	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {}
	
	public void onResume()
	{
		super.onResume();
		SelecterService.setMode(this, SelecterService.Mode.FULL_SELECT);
	}
}
